import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;

public class Main {
	/**
	 * Frame-ul principal
	 */
	public JFrame frame;
	/**
	 * Contine titlul aplicatiei
	 */
	public JLabel label;
	/**
	 * 
	 * Este zona in care se vor afisa actiunile sistemului in functie de timer
	 */
	public JTextArea texta1 ;
	/**
	 * Este zona in care se vor afisa actiunile din scenariu in functie de timer
	 */
	public JTextArea texta;
	/**
	 * Numarul intrebarilor de la preferinte
	 */
	public static int n=16;
	static String i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12,i13, i14;
	/**
	 * Atribut pentru a delimita incaperile la setarea preferintelor
	 */
	public static int f = 0;
    /**
     * O lista de tipul propozitie in care se pastreaza inregistrarile din scenariu
     */
	public List<Propozitie> Persoane;
	/**
	 * Un obiect de tipul timer2
	 */
	public timer2 countUp;
	/**
	 * Butonul deschide o noua fereastra in care are loc setarea preferintelor utilizatorilor
	 */
	public JButton button =new JButton("Seteaza preferintele");
	/**
	 * 
	 * Butonul realizeaza citirea fisierului de scenariu si introduce inregistrarile prin parametrii clasei Propozitie
	 */
	public JButton button1 =new JButton("Compileaza Scenariul");
	/**
	 * Butonul porneste sistemul propriu-zis:porneste timerul si afiseaza actiunile ce au loc in scenariu si deciziile pe care le ia sistemul
	 */
	public JButton button2 =new JButton("Porneste Sistemul");
	/**
	 * 
	 *Clasa ce contine interfata programului
	 */
	public Main(){
		Persoane=new ArrayList<Propozitie>();
		countUp=new timer2(Persoane);
		frame= new JFrame("SmartHouse");
		label = new JLabel("Sistem de recomandare pentru locuinte ");
		texta1 = new JTextArea(30, 30);
		texta = new JTextArea(30, 30);
	

		frame.setVisible(true);
		frame.setSize(1000,800);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel(new GridBagLayout());
		frame.getContentPane().add(panel,BorderLayout.NORTH);
		GridBagConstraints c = new GridBagConstraints();

		c.insets= new Insets(10,10,10,10);
		c.gridx=1;
		c.gridy=0;
		panel.add(label,c);
		
		c.gridx=0;
		c.gridy=2;
		panel.add(button,c);
		
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				final JFrame frame2 = new JFrame("Preferinte");
				frame2.setVisible(true);
				frame2.setSize(700, 400);

				JPanel panel2 = new JPanel(new GridBagLayout());
				frame2.getContentPane().add(panel2, BorderLayout.NORTH);
				GridBagConstraints d = new GridBagConstraints();

				JLabel label = new JLabel("Setarea Preferintelor:");
				d.gridx = 0;
				d.gridy = 0;
				d.insets = new Insets(10, 10, 10, 10);
				panel2.add(label, d);

				final JLabel intrebare = new JLabel(
				"Pentru cine setezi preferintele?");
				d.gridx = 0;
				d.gridy = 1;
				panel2.add(intrebare, d);

				//Create the radio buttons.
				final JRadioButton v1 = new JRadioButton("Ion");
				v1.setMnemonic(KeyEvent.VK_B);
				v1.setActionCommand("Ion");

				final JRadioButton v2 = new JRadioButton("Alina");
				v2.setMnemonic(KeyEvent.VK_C);
				v2.setActionCommand("Alina");
				v2.setSelected(true);

				final JRadioButton v3 = new JRadioButton("Ana");
				v3.setMnemonic(KeyEvent.VK_D);
				v3.setActionCommand("Ana");

				final JRadioButton v4 = new JRadioButton("Vlad");
				v4.setMnemonic(KeyEvent.VK_R);
				v4.setActionCommand("Vlad");


				ButtonGroup group = new ButtonGroup();
				group.add(v1);
				group.add(v2);
				group.add(v3);
				group.add(v4);

				JPanel radioPanel = new JPanel(new GridLayout(0, 1));
				radioPanel.add(v1);
				radioPanel.add(v2);
				radioPanel.add(v3);
				radioPanel.add(v4);

				d.gridx = 0;
				d.gridy = 3;
				panel2.add(radioPanel, d);
				//  panel2.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

				final JButton button1 = new JButton("Next");
				d.gridx = 0;
				d.gridy = 4;
				panel2.add(button1, d);

				final JButton button2 = new JButton("Salvare preferinte");
				d.gridx = 0;
				d.gridy = 5;
				panel2.add(button2, d);
				button2.setVisible(false);

				button2.addActionListener(new ActionListener() {

					public void actionPerformed(ActionEvent e) {

						try {
							PrintWriter fisierIesire = new PrintWriter(
									new FileWriter("preferinte.txt"), true);

							fisierIesire.println(i1+i2);
							fisierIesire.println(i1+i3);
							fisierIesire.println(i1+i4);
							fisierIesire.println(i1+i5);
							fisierIesire.println(i1+i6+i7);

							fisierIesire.println(i1+i8);
							if (i9!=null) fisierIesire.println(i1+i9);
							if (i10!=null) fisierIesire.println(i1+i10);
							if (i11!=null){
								if (i12!=null)
									fisierIesire.println(i1+i11+i12);
							}

							frame2.setVisible(false);
						} catch (IOException ee) {
							System.err.println("Eroare de fisier:"
									+ ee.toString());
							System.exit(1);
						}

					}

				});

				button1.addActionListener(new ActionListener() {

					public void actionPerformed(ActionEvent e) {

						if (n == 16) {
							intrebare
							.setText("La ce ora preferi sa te trezesti?");

							v1.setText("La ora 6:00");
							v2.setText("La ora 7:00");
							v3.setText("La ora 8:00");
							v4.setText("La ora 9:00");

							if (v1.isSelected()) {
								i1 = "Ion ";
							}
							if (v2.isSelected()) {
								i1 = "Alina ";
							}
							if (v3.isSelected()) {
								i1 = "Ana ";
							}
							if (v4.isSelected()) {
								i1 = "Vlad ";
							}

						}

						if (n == 15) {
							intrebare.setText("La ce ora preferi sa mananci?");

							v1.setText("La ora 12:00");
							v2.setText("La ora 13:00");
							v3.setText("La ora 14:00");
							v4.setText("La ora 15:00");

							if (v1.isSelected()) {
								i2 = "se trezeste la ora 6:00 ";
							}
							if (v2.isSelected()) {
								i2 = "se trezeste la ora 7:00 ";
							}
							if (v3.isSelected()) {
								i2 = "se trezeste la ora 8:00";
							}
							if (v4.isSelected()) {
								i2 = "se trezeste la ora 9:00";
							}

						}

						if (n == 14) {
							intrebare.setText("La ce ora preferi sa dormi?");

							v1.setText("La ora 21:00");
							v2.setText("La ora 22:00");
							v3.setText("La ora 22:30");
							v4.setText("La ora 23:00");

							if (v1.isSelected()) {
								i3 = "mananca de la ora 12:00 ";
							}
							if (v2.isSelected()) {
								i3 = "mananca de la ora 13:00";
							}
							if (v3.isSelected()) {
								i3 = "mananca de la ora 14:00";
							}
							if (v4.isSelected()) {
								i3 = "mananca de la ora 15:00 ";
							}

						}

						if (n == 13) {
							intrebare
							.setText("La ce ora preferi sa speli la masina de spalat?");
							v1.setText("La ora 16:00");
							v2.setText("La ora 16:30");
							v3.setText("La ora 17:30");
							v4.setText("La ora 18:00");

							if (v1.isSelected()) {
								i4 = "se culca la ora 21:00 ";
							}
							if (v2.isSelected()) {
								i4 = "se culca la ora 22:00 ";
							}
							if (v3.isSelected()) {
								i4 = "se culca la ora 22:30 ";
							}
							if (v4.isSelected()) {
								i4 = "se culca la ora 23:00 ";
							}

						}

						if (n == 12) {
							intrebare.setText("La ce ora preferi sa aspiri?");
							v1.setText("La ora 16:00");
							v2.setText("La ora 16:30");
							v3.setText("La ora 17:30");
							v4.setText("La ora 18:00");

							if (v1.isSelected()) {
								i5 = "spala haine de la ora 16:00";
							}
							if (v2.isSelected()) {
								i5 = "spala haine de la ora 16:30";
							}
							if (v3.isSelected()) {
								i5 = "spala haine de la ora 17:30";
							}
							if (v4.isSelected()) {
								i5 = "spala haine de la ora 18:00";
							}

						}

						if (n == 11) {
							intrebare.setText("Unde vrei sa aspiri?");
							v1.setText("In dormitor1.");
							v2.setText("In dormitor2.");
							v3.setText("In dormitor3.");
							v4.setText("In hol.");

							if (v1.isSelected()) {
								i6 = "aspira de la ora 16:00";
							}
							if (v2.isSelected()) {
								i6 = "aspira de la ora 16:30";
							}
							if (v3.isSelected()) {
								i6 = "aspira de la ora 17:30";
							}
							if (v4.isSelected()) {
								i6 = "aspira de la ora 18:00";
							}

						}

						if (n == 10) {
							intrebare
							.setText("Unde mergi dupa ce te trezesti?");

							v1.setText("Baie");
							v2.setText("Bucatarie");
							v3.setText("Living");
							v4.setVisible(false);

							if (v1.isSelected()) {
								i7 = " in dormitorul1";
							}
							if (v2.isSelected()) {
								i7 = " in dormitorul2";
							}
							if (v3.isSelected()) {
								i7 = " in dormitorul3";
							}
							if (v4.isSelected()) {
								i7 = " in hol";
							}

						}



						if (f == 0) {
							if (n == 9) {
								if (v1.isSelected()) {
									i8 = "merge in baie";
									f = 0;

								}
								if (v2.isSelected()) {
									f = 1;i8 = "merge in bucatarie";

								}
								if (v3.isSelected()) {
									f = 2;i8 = "merge in living";

								}
								intrebare.setText("Esti in baie. Alege o actiune.");


								v1.setText("Te speli pe maini?");
								v2.setText("Faci dus?");
								v3.setText("Folosesti toaleta? ");

								v4.setText("Te speli pe dinti?");
								v3.setVisible(true);
								v4.setVisible(true);


							}

						}

						if (n==8){

							if (v1.isSelected()) {
								i9 = "spala maini ";
							}
							if (v2.isSelected()) {
								i9 = "spala dus";
							}
							if (v3.isSelected()) {
								i9 = "merge la wc ";

							}

							if (v4.isSelected()) {
								i9 = "spala dinti ";
							}

							button1.setVisible(false);
							button2.setVisible(true);
						}


						if (f == 1) {


							if (n == 9) {
								if (v1.isSelected()) {
									i8 = "merge in baie";
									f = 0;

								}
								if (v2.isSelected()) {
									f = 1;i8 = "merge in bucatarie";

								}
								if (v3.isSelected()) {
									f = 2;i8 = "merge in living";

								}
								intrebare
								.setText("Ai ales sa mergi in bucatarie. Ce actiune alegi?");

								v1.setText("Vrei sa bei apa?");
								v2.setText("Mananci de la frigider?");
								v3.setText("Gatesti la aragaz?");
								v4.setVisible(false);
								n=n-1;
							}
						}
						if (n==7){
							if (v1.isSelected()) {
								i10 = "bea apa ";
							}
							if (v2.isSelected()) {
								i10 = "foloseste frigider ";
							}
							if (v3.isSelected()) {
								i10 = "foloseste aragaz ";
							}
							button1.setVisible(false);
							button2.setVisible(true);
						}
						if (f == 2) {
							if (n==9){
								if (v1.isSelected()) {
									i8 = "merge in baie";
									f = 0;
								}
								if (v2.isSelected()) {
									f = 1;i8 = "merge in bucatarie";
								}
								if (v3.isSelected()) {
									f = 2;i8 = "merge in living";
								}
								intrebare.setText  ("Ai ales sa mergi in living. Ce actiune alegi?");
								v1.setText              ("Te uiti la TV?");
								v2.setText  	 (" Folosesti calculatorul?");
								v3.setVisible(false);v4.setVisible(false);
								n=n-2;
							} 
						}
						if (n==6){
							intrebare.setText    (" De la ce ora?");
							v1.setText               ("13:00");
							v2.setText  	 	 ("14:00");
							v3.setText 	   (" 15:00");
							v4.setText ("16:00");
							v3.setVisible(true);
							v4.setVisible(true);
							if(v1.isSelected()){
								i11="folosesti tv "; 
							}
							if(v2.isSelected()){
								i11="folosesti calculatorul "; 
							}
						}
						if(n==5)
						{
							if(v1.isSelected()){
								i12=" de la 13:00";
							}
							if(v2.isSelected()){
								i12=" de la 14:00 ";
							}
							if(v3.isSelected()){
								i12=" de la 15:00";
							}
							if(v4.isSelected()){
								i12=" de la 16:00";
							}
							button1.setVisible(false);
							button2.setVisible(true);
						}
						n=n-1;						
					}
				});

			}
		});

		/**
		 * citeste scenariul si converteste in fapte jess
		 */
		
		c.gridx=1;
		c.gridy=2;
		panel.add(button1,c);
		button1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String pers="";
				String token = " ";
				String[] lcur;
				String act="", ora_start="",ora_end="",p_loc="",loc="";
				Propozitie tmp=null;
				Propozitie add1=new Propozitie("","","","","","","","","","");

				try{
					BufferedReader in=new BufferedReader(new FileReader("scenariu.txt"));
					String line;
					while((line=in.readLine())!= null){
						if(line.startsWith("Creaza")){
							pers = line.substring(7);
							Propozitie randCitit=new Propozitie(pers,"","","","","","","","","");
							Persoane.add(randCitit);}
						else{
							lcur=line.split(token);
							for(int i=0; i<Persoane.size();i++){
								tmp=Persoane.get(i);
								pers=tmp.getPersoana();
								if(pers.equals(lcur[0])){
									for(int j=1;j<lcur.length;j++){
										if(lcur[j].equals("in")){
											j++;
											loc=lcur[j];
											continue;	  
										}
										if(lcur[j].equals("de")){
											j+=3;
											ora_start = lcur[j];
											continue;
										}
										if(lcur[j].equals("la")&&(lcur[j+1].equals("ora"))){
											j+=2;
											ora_end = lcur[j];
											continue;
										}
										act = lcur[j]+" ";
									}
									break;
								}
							}
						}
						act=act.trim();//delete empty spaces
						if(act.equals("pleaca")||act.equals("vine")) {
							tmp.updateLocation("");
						}
						if(act.equals("merge")){
							if(!tmp.getLocation().equals("")){
								p_loc= tmp.getLocation();
							}
							tmp.updateLocation(loc);
						}
						/*if(loc.equals(""))
							loc=tmp.getLocation();*/
						if(ora_start.equals(""))
							ora_start=ora_end;
						String[] time1,time2;
						time1=ora_start.split(":");
						time2=ora_end.split(":");
						if((time1.length==3)&&(time2.length==3)){
							add1=new Propozitie(pers,act,p_loc,loc,time1[0],time1[1],time1[2],time2[0],time2[1],time2[2]);
							Persoane.add(add1);

						}else{

						}
					}

					//timer2 t2=new timer2(Persoane);
					//test only 
					for(Propozitie p: Persoane){
						System.out.print(p.getPersoana()+" ");
						System.out.print(p.getActiune()+" ");
						System.out.print(p.getPLocation()+" ");
						System.out.print(p.getLocation()+" ");
						System.out.print(p.getOraStart()+":");
						System.out.print(p.getMinStart()+":");
						System.out.print(p.getSecStart()+" ");
						System.out.print(p.getOraEnd()+":");
						System.out.print(p.getMinEnd()+":");
						System.out.print(p.getSecEnd()+" ");
						System.out.println();

					}

				}
				catch (IOException exc){
					System.err.println(exc);
				}
			}});

		//porneste sistemul
		
		c.gridx=2;
		c.gridy=2;
		panel.add(button2,c);
		button2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){    
				try{	
					countUp.start();
				}catch (Exception s){
					System.err.println("Error: " + s.getMessage());
				}
			}
		});

		//input scenariu
		JLabel label2 = new JLabel("Actiunile persoanelor: ");
		c.gridx=0;
		c.gridy=3;
		panel.add(label2,c);

		texta.setEditable(false);
		c.gridx=0;
		c.gridy=4;
		panel.add(texta,c);

		//output sistem
		JLabel label3 = new JLabel("Actiunile sistemului: ");
		c.gridx=1;
		c.gridy=3;
		panel.add(label3,c);

		texta.setEditable(false);
		c.gridx=1;
		c.gridy=4;
		panel.add(texta1,c);

		//timer
		c.gridx=2;
		c.gridy=5;
		panel.add(countUp,c);
	}
	

	public static void main(String[] args){
		new Main();
	}//main
}//Main





